const http = require('../../utils/request')
const { formatTime } = require('../../utils/formatTime')

Page({
  data: {
    currentStatus: 'all',
    orderList: [],
    filterOrderList: []
  },

  onLoad: function() {
    this.getOrderList();
  },

  filterOrders(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({
      currentStatus: status
    });
    // 添加根据状态筛选订单的逻辑
    this.filterOrdersByStatus(status);
  },
  getOrderList() {
    let userInfo = wx.getStorageSync("userInfo");
    http.get("/order/getAllOrders", { openID: userInfo.username}).then(res => {
      res.data.forEach(item => {
        item.time = formatTime(item.payment_time)
      });
      this.setData({
        orderList: res.data || [],
      }, () => {
        this.filterOrdersByStatus(this.data.currentStatus)
      })
    })
  },
  // 新增筛选订单的方法
  filterOrdersByStatus(status) {
    if (status === "all") {
      this.setData({
        filterOrderList: this.data.orderList
      })
    } else if (status === "pending") {
      this.setData({
        filterOrderList: this.data.orderList.filter(item => item.status === "2")
      })
    } else if (status === "processing") {
      this.setData({
        filterOrderList: this.data.orderList.filter(item => item.status === "3" || item.status === "4")
      })
    } else if (status === "completed") {
      this.setData({
        filterOrderList: this.data.orderList.filter(item => item.status === "5")
      })
    }
  },

  goToOrderDetail(e) {
    const orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?id=${orderId}`
    });
  },

  receivingOrders(e) {
    const orderId = e.currentTarget.dataset.id;
    let userInfo = wx.getStorageSync("userInfo");
    http.get("/order/acceptOrder", { openID: userInfo.username, orderId: orderId }).then(res => {
      // 处理接单逻辑
      wx.showToast({
        title: '接单成功',
        icon: 'success'
      });
      setTimeout(() => {
        this.getOrderList();
      }, 1000)
    })
  },

  completeOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    let userInfo = wx.getStorageSync("userInfo");
    http.get("/order/completeOrder", { openID: userInfo.username, orderId: orderId }).then(res => {
      // 处理接单逻辑
      wx.showToast({
        title: '接单成功',
        icon: 'success'
      });
      setTimeout(() => {
        this.getOrderList();
      }, 1000)
    })
  },
  deliveryOrder(e) {
    const orderId = e.currentTarget.dataset.id;
    let userInfo = wx.getStorageSync("userInfo");
    http.get("/order/deliverOrder", { orderId: orderId }).then(res => {
      let position = res.data[0]
      // 处理接单逻辑
      wx.openLocation({
        longitude: Number(position.longitude),
        latitude: Number(position.latitude),          
        name: position.address, // 位置名称
        success: function (res) {
          console.log('打开系统位置地图成功')
        },
        fail: function (error) {
          wx.showToast({
            title: '请安装腾讯地图',
            icon: 'success'
          });
        }
      })
    })
  }
});
